package com.youkeda.service.impl;

import com.youkeda.model.Page;
import com.youkeda.model.Student;
import com.youkeda.service.StudentService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class StudentServiceImpl implements StudentService {
    List<Student> students;

    @Override
    public void add(Student student) {
        if (students == null) {
            students = new ArrayList<>();
        }
        students.add(student);
    }

    @Override
    public List<Student> getAll() {
        return students;
    }

    @Override
    public Page<Student> query(int pageNumber) {
        long totalElements = students.size();
        int pageSize = 3;
        int totalPage;
        if (totalElements % pageSize == 0) {
            totalPage = (int) (totalElements / pageSize);
        } else {
            totalPage = (int) (totalElements / pageSize + 1);
        }
        int begin = (pageNumber - 1) * pageSize;
        int end;
        if (pageNumber == totalPage) {
            end = (int) totalElements;
        } else {
            end = begin + pageSize;
        }
        List<Student> studentList = students.subList(begin, end);
        Page<Student> page = new Page<>(pageSize, totalPage, totalElements, pageNumber, studentList);
        return page;
    }
}
